package dame1;

import java.util.*;

public class Binarytree {
    static class TreeNode {
        public char val;
        public TreeNode left;  //存储左孩子结点
        public TreeNode right; //存储右孩子结点

        public TreeNode(char val) {
            this.val = val;
        }

    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }


    int i = 0;
    public TreeNode createTree(String str){          //创建二叉树
        TreeNode root = null;
        if(str.charAt(i) != '#'){
            root  = new TreeNode(str.charAt(i));
            i++;
        }else {
            i++;
        }
        return root;
    }
    public void preOrder(TreeNode root) {  //前序遍历
        if (root == null) return;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void preOrderNor(TreeNode root){  //非递归前序遍历
        if (root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null||!stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    public void inOrder(TreeNode root) {  //中序遍历
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public void inOrderNor(TreeNode root){  //非递归中序遍历
        if (root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null||!stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    public void postOrder(TreeNode root) {  //后序遍历
        if (root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public void postOrderNor(TreeNode root){   //非递归后序遍历
        if (root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur!=null||!stack.isEmpty())
        while (cur != null){
            stack.push(cur);
            cur = cur.left;
        }
        TreeNode top = cur.right;
        if (top.right == null){
            System.out.println(top.val + " ");
            stack.pop();
        }else {
            cur = top.right;
        }
    }


    public void leveOrder(TreeNode root){     //二叉树的层序遍历1
        if (root==null){
            return;
        }
        TreeNode cur = root;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            cur = queue.poll();
            System.out.println(cur.val + " ");
            if (cur.left != null){
                queue.offer(cur.left);
            }
            if (cur.right != null){
                queue.offer(cur.right);
            }
        }
    }


    public List<List<Character>> levelOrder2(TreeNode root) {   //二叉树的层序遍历2
        List<List<Character>> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        TreeNode cur = root;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while (size!=0) {
                cur = queue.poll();
                list.add(cur.val);
                //System.out.println(cur.val + " ");
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }

        static int nodesiez;

    public void size(TreeNode root) {   //求结点个数
        if (root == null) return;
        nodesiez++;
        size(root.left);
        size(root.right);
    }

    public int size2(TreeNode root) {   //求结点个数
        if (root == null) {
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }


    static int size;

    public void getLeafNodeCount(TreeNode root) {  //获取叶子节点的个数
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            size++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }

    public int getLeafNodeCount2(TreeNode root) {  //获取叶子节点的个数
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) +
                getLeafNodeCount2(root.right);
    }

    public int getKLevelNodeCount(TreeNode root, int k) {   //求第K层节点的个数
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) +
                getKLevelNodeCount(root.right, k - 1);
    }

    public  int getHeight(TreeNode root) {    // 获取二叉树的高度  时间复杂度O(N)
        if (root==null) return 0;             //空间复杂度：O(logN);
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        /*return Math.max(leftHeight
        ,rightHeight)+1;*/
        return leftHeight>rightHeight
                ?leftHeight+1:rightHeight+1;
    }

    public TreeNode findval(TreeNode root, int val){     // 检测值为value的元素是否存在
        if (root==null) {
            return null;
        }
        if (root.val==val){
            return root;
        }
        TreeNode leftT = findval(root.left,val);
            if (leftT.val!=val){
                return leftT;
            }
        TreeNode rightT = findval(root.left,val);
        if (rightT.val!=val){
            return rightT;
        }
        return null;
    }

    public boolean isCompleteTree(TreeNode root){     // 判断一棵树是不是完全二叉树
        return false;
    }


    public boolean isSameTree(TreeNode p, TreeNode q) {  //判断两棵树是否相同
        //先判断树的结构是否一样
        if (p==null&&q!=null||p!=null&&q==null){      //p节点 m个， q节点 s个
            return false;                             //时间复杂度O(min(m,s);
        }
        if (p==null&&q==null){
            return true;
        }
        //都不为空判断值是否一样
        if (p.val !=q.val){
            return false;
        }
        //都不为空且值一样
        return isSameTree(p.left,q.left)
                && isSameTree(p.right,q.right);
    }

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {  //另一棵树的子树
        if (root==null) return false;
        if (isSameTree(root,subRoot)) return true;          //root节点 r个，subRoot节点 s个
        if(isSubtree(root.left,subRoot)) return  true;     // 时间复杂度 O(r*s);
        if (isSubtree(root.right,subRoot)) return true;
        return false;
    }


    public TreeNode invertTree(TreeNode root) {   //翻转二叉树
        if(root == null){
            return null ;
        }
        if(root.left==null&&root.right==null){
            return root;
        }
        TreeNode cur = root.left;
        root.left = root.right;
        root.right = cur;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }


    public boolean isSymmetric(TreeNode root) {        //判断一棵树是否为对称二叉树
        if(root==null){
            return true;
        }
        return isSymmetricChild(root.left,root.right);
    }
    public boolean isSymmetricChild(TreeNode leftTree , TreeNode rightTree){
        if(leftTree!=null&&rightTree==null||leftTree==null&&rightTree!=null){
            return false;
        }
        if(leftTree==null&&rightTree==null){
            return true;
        }
        if(leftTree.val != rightTree.val){
            return false;
        }
        return isSymmetricChild(leftTree.left,rightTree.right)
                &&isSymmetricChild(leftTree.right,rightTree.left);
    }


    public boolean isBalanced(TreeNode root) {  //判断是否为平衡二叉树  时间复杂度O(N*N);
        if(root==null) return true;
        int leftHeight= getHeight1(root.left);
        int rightHeight = getHeight1(root.right);
        return Math.abs(leftHeight-rightHeight)<2 &&isBalanced(root.left) && isBalanced(root.right);
    }
    public  int getHeight1(TreeNode root) {    // 获取二叉树的高度  时间复杂度O(N)
        if (root==null) return 0;             //空间复杂度：O(logN);
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        /*return Math.max(leftHeight
        ,rightHeight)+1;*/
        return leftHeight>rightHeight
                ?leftHeight+1:rightHeight+1;
    }

    public boolean isBalanced2(TreeNode root) {  //判断是否为平衡二叉树第二种方法  时间复杂度O(N);
        if(root==null) return true;
        return getHeight(root)>=0;
    }
    public  int getHeight2(TreeNode root) {    // 获取二叉树的高度  时间复杂度O(N)
        if (root==null){
            return 0;
        }             //空间复杂度：O(logN);
        int leftHeight = getHeight(root.left);
        if(leftHeight<0){
            return -1;
        }
        int rightHeight = getHeight(root.right);
        if(rightHeight>=0 && Math.abs(leftHeight-rightHeight)<=1){
            return Math.max(leftHeight,rightHeight) + 1;
        }else{
            return -1;
        }
    }



    public  boolean isCompleTree(TreeNode root){   //判断是否为完全二叉树
        if (root==null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while (!queue.isEmpty()){
            TreeNode peek = queue.peek();
            if (peek!=null){
                return  false;
            }
            queue.poll();
        }
        return true;
    }


    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {  //二叉树最近公共祖先
        if(root == null){                                                         //画图调试(难理解)
            return null;
        }
        if(root==p||root==q){
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rigthTree = lowestCommonAncestor(root.right,p,q);
        if(leftTree!=null && rigthTree!=null){
            return root;
        }else if(leftTree!=null){
            return leftTree;
        }else{
            return rigthTree;
        }
    }


   /* public TreeNode buildTree(int[] preorder, int[] inorder) {//前序和中序创建二叉树

    }*/



    public String tree2str(TreeNode root) {     //根据二叉树创建字符串
        if(root==null){
            return null;
        }
        StringBuilder stringbuilder = new StringBuilder();
        tree2strChild(root,stringbuilder);
        return stringbuilder.toString();
    }
    public void tree2strChild(TreeNode t , StringBuilder stringbuilder) {
        if(t==null) return;
        stringbuilder.append(t.val);
        if(t.left!=null){
            stringbuilder.append("(");
            tree2strChild(t.left,stringbuilder);
            stringbuilder.append(")");
        }else{
            if(t.right==null){
                return;
            }else{
                stringbuilder.append("()");
            }
        }
        if(t.right!=null){
            stringbuilder.append("(");
            tree2strChild(t.right,stringbuilder);
            stringbuilder.append(")");
        }else{
            return;
        }
    }





}

